/**
 * Narato theme settings
 * @author Levin
 * @version 1.0
 */
var NaratoCfg=(function($){
/* begin */

	var p={},pub={};

	/* private methods
	+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
	p.G=function(id){
		return document.getElementById(id);
	};
	p.tabNavi={
		init:function(){
			// show link select box if navigation is set to show links
			$('#opt_navigation').change(function() {
				$('.opt_links, #page-list, #category-list').hide();
				if ($(this).find('option:selected').attr('value') != '0') {
					$('#nav-list').show();
				} else { 
					$('#nav-list').hide();
				};
				if ($(this).find('option:selected').attr('value') == 'links') {
					$('.opt_links').show();
				};
				if ($(this).find('option:selected').attr('value') == 'pages') { 
					$('#page-list').show();
				};
				if ($(this).find('option:selected').attr('value') == 'categories') {
					$('#category-list').show();
				};
			});
			$("#opt_navigation").change();		
		}
	};
	p.tabAdvance={
		init:function(){
			//whether enable jquery
			$('#opt_jquery').change(function() {
				$('#opt_lightbox,#opt_ajax_comments').attr("disabled", true);
				if ($(this).is(":checked")) {
					$('#opt_lightbox, #opt_ajax_comments').attr("disabled", false);
				}
			});
			$("#opt_jquery").change();		
		}
	};
	p.tabContent={
		init:function(){
			$('#opt_post_content').change(function() {
				$('#opt_post_content_length').attr("disabled", true);
				if ($(this).is(":checked")) {
					$('#opt_post_content_length').attr("disabled", false);
				}
			});
			$("#opt_post_content").change(); 	
		}
	};

	p.preview={
		sliderFlag:null,
		initSlider:function(){
			var me=this;
			//only one column
			if(p.opts.layout_type=='g-col1'){
				$("#dimension_controls").hide();
				$('#main', themepreview.document).css({
					'margin-left':'auto',
					'margin-right':'auto'
				});
				return;
			};
			//destroy last slider
			if(me.sliderFlag){
				$(me.sliderFlag).appendTo("#slider-wrap");
			};
			
			$("#slider").html("");
			//multi-columns layout
			me.sliderFlag="#opt_dimensions_" + p.opts.layout + "_" + p.opts.layout_type;
			$(me.sliderFlag).appendTo("#slider").slider({
				from: 0,
				to: p.opts.gs,
				step: p.opts.jstep,
				dimension: p.opts.unit,
				scale: p.opts.jscale,
				limits: false,
				onstatechange: function(_val) {
					//alert(val);
					var s = _val.split(';'),
						gutter=p.opts.gutter[p.opts.layout];
					s[0] = parseInt(s[0]);
					s[1] = parseInt(s[1]);
					switch (p.opts.layout_type) {
						case 'g-col2-mr':
							$('#main', themepreview.document).css({
								'margin-left': s[0] + p.opts.unit,
								'margin-right':'auto'
							});
							$('#main .col-m-inner',themepreview.document).css({
								'margin-left':'auto',
								'margin-right':'auto'
							});
							$('#aside-x', themepreview.document).css({
								'width': (s[0]-gutter) + p.opts.unit,
								'margin-left':'auto',
								'margin-right':'auto'
							});
							break;
						case 'g-col2-ml':
							$('#main', themepreview.document).css({
								'margin-right': (p.opts.gs - s[0]) + p.opts.unit,
								'margin-left':'auto'
							});
							$('#main .col-m-inner',themepreview.document).css({
								'margin-left':'auto',
								'margin-right':'auto'
							});
							$('#aside-x', themepreview.document).css({
								'width': (p.opts.gs - s[0]-gutter) + p.opts.unit,
								'margin-left':'auto',
								'margin-right':'auto'
							});
							break;
						case 'g-col3-mm':
							$('#main', themepreview.document).css({
								'margin-left':'auto',
								'margin-right':'auto'
							});
							$("#main .col-m-inner",themepreview.document).css({
								'margin-left':(p.opts.gs - s[1]+gutter/2) + p.opts.unit,
								'margin-right':(s[0]+gutter/2) + p.opts.unit
							});
							$('#aside-x', themepreview.document).css({
								'width': (p.opts.gs - s[1]) + p.opts.unit,
								'margin-left': '-100%',
								'margin-right':'auto'
							});
							$('#aside-y', themepreview.document).css({
								'width': s[0] + p.opts.unit,
								'margin-left': -(p.opts.gs - s[1]) + p.opts.unit,
								'margin-right':'auto'
							});
							break;
						case 'g-col3-mr':
							$('#main', themepreview.document).css({
								'margin-left': s[1] + p.opts.unit,
								'margin-right':'auto'
							});
							$('#main .col-m-inner',themepreview.document).css({
								'margin-left':'auto',
								'margin-right':'auto'
							});
							$('#aside-x', themepreview.document).css({
								'width': s[0] + p.opts.unit,
								'margin-left':'auto',
								'margin-right':'auto'
							});
							$('#aside-y', themepreview.document).css({
								'width': (s[1] - s[0]-gutter) + p.opts.unit,
								'margin-left':gutter/2+p.opts.unit,
								'margin-right':'auto'
							});
							break;
						case 'g-col3-ml':
							$('#main', themepreview.document).css({
								'margin-right': (p.opts.gs - s[0]) + p.opts.unit,
								'margin-left':'auto'
							});
							$('#main .col-m-inner',themepreview.document).css({
								'margin-left':'auto',
								'margin-right':'auto'
							});
							$('#aside-x', themepreview.document).css({
								'width': (p.opts.gs - s[1]) + p.opts.unit,
								'margin-left':'auto',
								'margin-right':'auto'
							});
							$('#aside-y', themepreview.document).css({
								'width': (s[1] - s[0]-gutter) + p.opts.unit,
								'margin-right':gutter/2+p.opts.unit,
								'margin-left':'auto'
							});
							break;
					};//switch
					return _val;
				}//onstatechange
			});//slider
			$("#dimension_controls").show();
		},//setSlider
		initColorPicker:function(){
			// colors
			$('.color-selector').ColorPicker({
				onShow: function(colpkr) {
					$(colpkr).css({
						opacity: 0,
						marginTop: -10
					}).show().animate({
						opacity: 1,
						marginTop: 0
					},
					160);
					$(this).ColorPickerSetColor($(this).find('input').val());
					return false;
				},
				onHide: function(colpkr) {
					$(colpkr).fadeOut(160);
					return false;
				},
				onChange: function(hsb, hex, rgb, el) {
					el=$(el).find(".preview").css('background-color', '#' + hex);
					$input = el.find('input');
					$input.val(hex);
					if (hex != '000000') {
						p.G('themepreview').contentWindow.$.cssRule('body', "background-color", "#" + hex);
						p.G('themepreview').contentWindow.$.cssRule((p.opts.bg==''?'body,#page':'#page'), "background-image", "none");
					} else { // pretty ugly, need to improve this
						p.G('themepreview').contentWindow.$.cssRule('body', "background-color", "#000");
						if(p.opts.bg==''){
							p.G('themepreview').contentWindow.$.cssRule('body', "background-image", "url("+p.opts.url_theme+"/images/bg.png)");
							p.G('themepreview').contentWindow.$.cssRule('#page', "background-image", "url("+p.opts.url_theme+"/images/header.jpg)"); 							
						};
					};
				}//onChange
			});//colorpicker		
		},
		initFontStyle:function(){
			// font style check
			$('#opt_font_style').change(function() {
				var $sel = $(this).find('option:selected').attr('value'),
					cnt=p.opts.fonts.length;
				for(var i=0;i<cnt;i++ ){
					if($sel==p.opts.fonts[i].i){
						$('*', themepreview.document).css('font-family',p.opts.fonts[i].v);
						break;
					};
				};//for
			}); 		
		},//initFontStyle
		initColorScheme:function(){
			$('#color-scheme input[type=radio]').change(function() {
				var $sel = $(this).attr('value'),
					html='<style type="text/css">@import url("'+p.opts.url_theme+'/color-' + $sel + '.css"); </style>';
				$('body', themepreview.document).append(html);
			});
			$("#color-scheme input[type=radio]:checked").change();		
		},
		initLayoutType:function(){
			var me=this;
			$('#layout-settings input[type=radio]').change(function() {
				// remove old class
				$('#page', themepreview.document).removeClass(p.opts.layout_type); 
				p.opts.layout_type = $(this).val();
				// add new one
				$('#page', themepreview.document).addClass(p.opts.layout_type); 
				// reset range slider
				me.initSlider(); 
				return true;
			});		
		},
		initPageWith:function(){
			var me=this;
			$('input[name=page_width]').change(function() {
				var val=$(this).val();
				if (val == 'fluid') {
					p.opts.unit = '%';
					p.opts.gs = 100;
					p.opts.jstep = 1;
					p.opts.jscale = p.opts.scale_fluid;
					p.opts.layout =val;
				} else {
					p.opts.layout =val;
					p.opts.unit = 'px';
					p.opts.gs = 980;
					p.opts.jstep = 10;
					p.opts.jscale = p.opts.scale_grid;
				};
				// reset range slider
				me.initSlider(); 
				// remove old class
				$('body', themepreview.document).removeClass((val == 'fluid') ? "fixed": "fluid"); 
				// add new one
				$('body', themepreview.document).addClass((val == 'fluid') ? "fluid": "fixed"); 
				return true;
			});		
		},//initPageWith
		onOk:function(html){
			var me=this;

			$("#themepreview-wrap").html(html);
			$("#themepreview-wrap").show();
			$('#themepreview-wrap:not(.processed)').TextAreaResizer(); 
			// wait for load because of IE problems...
			$('#themepreview').load(function() { 
				// remove links to other pages from iframe doc.
				$('#themepreview').contents().find("a").each(function() {
					var $href = $(this).attr('href');
					if ($href && $href.indexOf("#") != 0) $(this).attr("href", "#");
				}); 
				
				me.initPageWith();
				me.initLayoutType();
				me.initColorScheme();
				me.initSlider();
				me.initFontStyle();
				me.initColorPicker();

			});//load

		},//onOk
		init:function(){
			var me=this;
			$.ajax({
				type: "post",
				url: "admin-ajax.php",
				data: {
					action: 'site_preview',
					_ajax_nonce:p.opts.ajax_nonce
				},
				beforeSend: function() {
					$("#themepreview-wrap .loading").show("slow");
				},
				complete: function() {
					$("#themepreview-wrap .loading").hide("fast");
				},
				success: function(html) {
					me.onOk(html);
				}//success
			});//$.ajax			
		}
	};

	p.init=function(){
		$(document).ready(function() { 
			// set up tabs
			$("#narato-settings-tabs").minitabs({
				contentClass: '.sections',
				speed: 333
			}); 
			p.tabNavi.init();
			p.tabAdvance.init();
			p.tabContent.init();

			// not on IE
			if ($.support.leadingWhitespace) {
				$('#layout-settings, #color-scheme').radio2select();
			};

			//preview
			p.preview.init();

			// make all textareas resizable
			$('.ad-code:not(.processed)').TextAreaResizer();

		});//ready
		
	};//p.init

	/* public methods
	+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
	pub.Init=function(opts){
		p.opts=$.extend({
			scale_grid:['0', '|', '|', '80', '|', '|', '|', '140', '|', '|', '|', '200', '|', '|', '|', '260', '|', '|', '|', '320', '|', '|', '|', '380', '|', '|', '|', '440', '|', '|', '|', '500', '|', '|', '|', '560', '|', '|', '|', '620', '|', '|', '|', '680', '|', '|', '|', '740', '|', '|', '|', '800', '|', '|', '|', '860', '|', '|', '|', '920', '|', '|', '|', '980'],
			scale_fluid:['0', '|', '10', '|', '20', '|', '30', '|', '40', '|', '50', '|', '60', '|', '70', '|', '80', '|', '90', '|', '100'],
			gutter:{
				"fixed":25,//25px
				"fluid":5  //5%
			}
		},opts||{});
		//page width unit
		p.opts.unit=(p.opts.layout == 'fluid' ? '%' : 'px');
		//page width
		p.opts.gs=(p.opts.layout == 'fluid' ? 100 : 980);
		p.opts.jstep=(p.opts.layout == 'fluid' ? 1 : 10);
		p.opts.jscale=(p.opts.layout == 'fluid' ? p.opts.scale_fluid : p.opts.scale_grid);
		
		p.init();
	};
	return pub;

/* end */
})(jQuery);